package com.ibo.htlivecamera.service;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;

import com.ibo.htlivecamera.app.MyApplication;

import java.util.List;

public class ServiceManager {
    public static void startUpService(Context context) {
        if (isServiceRunning(context, "com.ibo.htlivecamera.service.SocketService")) {
            System.out.println("#######   isServiceRunning !!!");
            return;
        }
        System.out.println("#######   startUpService !!!");
        context.startService(new Intent(context, SocketService.class));
    }


    public static void stopService(Context context) {
        System.out.println("#######   stopService !!!");
        context.stopService(new Intent(context, SocketService.class));
    }


    public static boolean isServiceRunning(Context context, String className) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(Integer.MAX_VALUE);
        if (!(serviceList.size() > 0)) {
            return false;
        }
        for (int i = 0; i < serviceList.size(); i++) {
            ActivityManager.RunningServiceInfo serviceInfo = serviceList.get(i);
            ComponentName serviceName = serviceInfo.service;

            if (serviceName.getClassName().contains(className)) {
                return true;
            }
        }
        return false;
    }


    public static void moveToFront() {
        if (isRunningForeground()) {
            System.out.println("###### isRunningForeground!!!  ");
            return;
        }
        ActivityManager manager = (ActivityManager) MyApplication.getInstance().getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> recentTasks = manager.getRunningTasks(Integer.MAX_VALUE);
        for (int i = 0; i < recentTasks.size(); i++) {
            if (recentTasks.get(i).baseActivity.toShortString().contains("com.ibo.htlivecamera.MainActivity")) {
                System.out.println("##########  moveTaskToFront !!!  " + recentTasks.get(i).id);
                manager.moveTaskToFront(recentTasks.get(i).id, ActivityManager.MOVE_TASK_WITH_HOME);
            }
        }
    }

    public static boolean isRunningForeground() {
        ActivityManager activityManager = (ActivityManager) MyApplication.getInstance().getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcessInfos = activityManager.getRunningAppProcesses();
        // 枚举进程
        for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessInfos) {
            if (appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                if (appProcessInfo.processName.contains(MyApplication.getInstance().getApplicationInfo().processName)) {
                    return true;
                }
            }
        }
        return false;
    }

}
